package com.dream.dao.base;

/**
 * 
 * @author Evan
 * @date 2013-8-25
 * 
 */
import java.util.List;

import org.hibernate.criterion.Projections;
import org.springframework.stereotype.Repository;

import com.dream.model.app.AppGroup;
import com.dream.utils.hibernate.DetachedCriteriaBuilder;
import com.dream.utils.hibernate.DreamBaseDAO;
import com.dream.utils.hibernate.PagingVO;

/**
 * 
 * @copyright evan
 * @author evan
 * @Revision
 * @date 2013-9-8
 */
@Repository
public class AppGroupDAO extends DreamBaseDAO<AppGroup> {
	
	/**
	 * 
	 * @author evan
	 * @Revision
	 * @date 2013-9-8
	 * @param name
	 * @return
	 */
	public AppGroup getByName(final String name){
		
		DetachedCriteriaBuilder builder = DetachedCriteriaBuilder.instance(AppGroup.class);
		builder.addEq("deleted", false).addEq("name", name);
		return selectTopOne(builder);
	}
	
	/**
	 * 
	 * @author evan
	 * @Revision
	 * @date 2013-9-8
	 * @param id
	 * @return
	 */
	public Integer delete(final String id){
		
		final String  DEL_GRP = "delete AppGroup where id=?";
		return bulkUpdate(DEL_GRP, id);
		
	}
	
	/**
	 * 
	 * @author evan
	 * @Revision
	 * @date 2013-9-8
	 * @param pageParam
	 * @return
	 */
	public PagingVO query(final PagingVO vo){
		
		DetachedCriteriaBuilder builder = DetachedCriteriaBuilder.instance(AppGroup.class);
		builder.setOrder(vo.getOrderName());
		List<AppGroup> data = selectPaging(builder, vo);
		vo.setCount(count());
		vo.setDetails(data);
		return vo;
	}
	
	/**
	 * 
	 * @author evan
	 * @Revision
	 * @date 2013-9-8
	 * @return
	 */
	private  Long count(){
		
		DetachedCriteriaBuilder builder = DetachedCriteriaBuilder.instance(AppGroup.class);
		builder.setProjection(Projections.rowCount());
		return count(builder);
	}
	
	/**
	 * 
	 * @author evan
	 * @Revision
	 * @date 2013-9-8
	 * @param groupId
	 * @return
	 */
	public List<AppGroup> detail(final String groupId) {
		
		DetachedCriteriaBuilder builder = DetachedCriteriaBuilder.instance(AppGroup.class);
		builder.leftJoin("groupResources", "gr");
		builder.addEq("deleted", false).addEq("gr.deleted", false);
		return this.select(builder);
	}
}
